package com.androiddatasender;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{
    private Handler handler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        Button btnConnect = (Button) findViewById(R.id.btnConnect);
        if (btnConnect != null)
        {
            btnConnect.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    String ip = "";
                    short port = 0;
                    EditText txtIP = (EditText) findViewById(R.id.txtIP);
                    if (txtIP != null)
                    {
                        ip = txtIP.getText().toString();
                    }

                    EditText txtPort = (EditText) findViewById(R.id.txtPort);
                    if (txtPort != null)
                    {
                        port = Short.parseShort(txtPort.getText().toString());
                    }

                    if (ip.length() > 0 && port != 0)
                    {
                        Connector connect = new Connector(ip, port);
                        Thread t = new Thread(connect);
                        t.start();
                    }
                }
            });
        }

        Button btnSend = (Button) findViewById(R.id.btnSend);
        if (btnSend != null)
        {
            btnSend.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    send();
                }
            });
        }
    }

    private class Connector implements Runnable
    {
        private String ip = "";
        private short port = 0;

        public Connector(String ip, short port)
        {
            this.ip = ip;
            this.port = port;
        }

        @Override
        public void run()
        {
            connect(ip, port);
        }
    }

    public void connect(String ip, short port)
    {
        try
        {
            Configuration conf = new Configuration();
            conf.ip = ip;
            conf.port = port;
            conf.transport = Configuration.TRANSPORT_TCP;
            conf.mode = Configuration.MODE_SENDER;
            Callback callback = new Callback();
            Net.getInstance().receiveCallback(callback);

            Net.getInstance().initialize(conf);
            handler.post(new Runnable()
            {
                @Override
                public void run()
                {
                    Button btnConnect = (Button) findViewById(R.id.btnConnect);
                    btnConnect.setEnabled(false);
                    new Sender().start();
                }
            });
            Net.getInstance().loop();
        } catch (Exception e)
        {
        } finally
        {
            Net.getInstance().shutdown();
        }
    }

    public class Sender extends Thread
    {
        public void run()
        {
            for (; ; )
            {
                try
                {
                    send();
                    Thread.sleep(1);
                } catch (InterruptedException ire)
                {
                }
            }
        }
    }

    public void send()
    {
        Msg msg = new Msg();
//        byte[] payload = new byte[5];
        byte[] payload = new byte[128 * 96 * 3];
        for (int i = 0; i < payload.length; i += 1)
        {
            payload[i] = 8;
        }
        payload[0] = 2;
        payload[payload.length - 1] = 3;
        msg.add(4, payload);
        Net.getInstance().send(msg);
    }

    private class Callback implements ReceiveCallback
    {
        @Override
        public void onReceive(Context context, Msg msg)
        {
        }
    }
}
